package test_public;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import datamodel.rucksack.Rucksack;
import datamodel.rucksack.RucksackObject;
import datamodel.tree.Tree;
import datamodel.tree.TreeNode;
import datamodel.tree.linked.LinkedTree;
import datamodel.tree.linked.LinkedTreeNode;
import datamodel.tree.sequential.SequentialTree;
import datamodel.tree.sequential.SequentialTreeNode;

public class TreeTest {
	
	Tree sequentialTree = new SequentialTree();
	
	RucksackObject o1 = new RucksackObject(10, 20);
	RucksackObject o2 = new RucksackObject(20, 40);
	RucksackObject o3 = new RucksackObject(40, 80);
	RucksackObject o4 = new RucksackObject(80, 160);
	RucksackObject o5 = new RucksackObject(160, 320);
	RucksackObject o6 = new RucksackObject(320, 640);
	RucksackObject o7 = new RucksackObject(640, 1280);
	RucksackObject o8 = new RucksackObject(1280, 2560);
	RucksackObject o9 = new RucksackObject(2560, 5120);
	RucksackObject o10 = new RucksackObject(5120, 10240);
	RucksackObject o11 = new RucksackObject(10240, 20480);
	RucksackObject o12 = new RucksackObject(20480, 40960);
	
	@Before
	public void before() {
		Rucksack rucksack = new Rucksack(100);
		rucksack.insert(o1);
		TreeNode node = new SequentialTreeNode(rucksack);
		
		sequentialTree.setCurrentNode(node);
		
		rucksack = new Rucksack(100);
		rucksack.insert(o2);
		node = new SequentialTreeNode(rucksack);
		
		sequentialTree.setLeftNode(node);
		
		rucksack = new Rucksack(100);
		rucksack.insert(o3);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.moveToLeftNode();
		sequentialTree.setRightNode(node);

		rucksack = new Rucksack(100);
		rucksack.insert(o4);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.moveToRoot();
		sequentialTree.setRightNode(node);
	}

	@Test
	public void testEquals() {
		LinkedTree linkedTree = new LinkedTree();
		Rucksack rucksack = new Rucksack(100);
		
		rucksack.insert(o1);
		TreeNode node = new LinkedTreeNode(rucksack);
		
		linkedTree.setCurrentNode(node);
		
		rucksack = new Rucksack(100);
		rucksack.insert(o2);
		node = new LinkedTreeNode(rucksack);
		
		linkedTree.setLeftNode(node);
		
		rucksack = new Rucksack(100);
		rucksack.insert(o3);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToLeftNode();
		linkedTree.setRightNode(node);

		rucksack = new Rucksack(100);
		rucksack.insert(o4);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToRoot();
		linkedTree.setRightNode(node);

		Assert.assertEquals(linkedTree, sequentialTree);
	}
	
	@Test
	public void testNull() {
		Assert.assertFalse(sequentialTree.equals(null));		
	}
	
	
	@Test
	public void testNotEqual() {
		LinkedTree linkedTree = new LinkedTree();
		Rucksack rucksack = new Rucksack(100);
		
		rucksack.insert(o1);
		TreeNode node = new LinkedTreeNode(rucksack);
		
		linkedTree.setCurrentNode(node);
		
		rucksack = new Rucksack(100);
		rucksack.insert(o2);
		node = new LinkedTreeNode(rucksack);
		
		linkedTree.setLeftNode(node);
		
		rucksack = new Rucksack(100);
		rucksack.insert(o3);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToLeftNode();
		linkedTree.setRightNode(node);

		rucksack = new Rucksack(100);
		rucksack.insert(o4);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToRoot();
		linkedTree.setRightNode(node);

		Assert.assertTrue(sequentialTree.equals(linkedTree));
	}

	@Test
	public void otherTest() {
		sequentialTree.clearTree();
		Rucksack rucksack = new Rucksack(100000);
		rucksack.insert(o1);
		TreeNode node = new SequentialTreeNode(rucksack);

		sequentialTree.setCurrentNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o2);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.setLeftNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o3);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.moveToLeftNode();
		sequentialTree.setRightNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o4);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.moveToRoot();
		sequentialTree.setRightNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o5);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.moveToRightNode();
		sequentialTree.setRightNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o6);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.moveToRightNode();
		sequentialTree.setLeftNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o7);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.moveToLeftNode();
		sequentialTree.moveToRightNode();
		sequentialTree.setLeftNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o8);
		node = new SequentialTreeNode(rucksack);

		sequentialTree.moveToRightNode();
		sequentialTree.moveToRightNode();
		sequentialTree.moveToRightNode();
		sequentialTree.setRightNode(node);
	}
	
	@Test
	public void testEquals2() {
		LinkedTree linkedTree = new LinkedTree();
		Rucksack rucksack = new Rucksack(100000);

		rucksack.insert(o1);
		TreeNode node = new LinkedTreeNode(rucksack);

		linkedTree.setCurrentNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o2);
		node = new LinkedTreeNode(rucksack);

		linkedTree.setLeftNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o3);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToLeftNode();
		linkedTree.setRightNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o4);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToRoot();
		linkedTree.setRightNode(node);

		rucksack = new Rucksack(100000);

		rucksack.insert(o5);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToRightNode();
		linkedTree.setRightNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o6);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToRightNode();
		linkedTree.setLeftNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o7);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToLeftNode();
		linkedTree.moveToRightNode();
		linkedTree.setLeftNode(node);

		rucksack = new Rucksack(100000);
		rucksack.insert(o8);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToRightNode();
		linkedTree.moveToRightNode();
		linkedTree.moveToRightNode();
		linkedTree.setRightNode(node);

		Assert.assertFalse(sequentialTree.equals(linkedTree));
	}
	
	@Test
	public void testEmptyRucksack() {
		LinkedTree linkedTree = new LinkedTree();
		Rucksack rucksack = new Rucksack(100);
		
		TreeNode node = new LinkedTreeNode(rucksack);
		
		linkedTree.setCurrentNode(node);
		
		rucksack = new Rucksack(100);
		node = new LinkedTreeNode(rucksack);
		
		linkedTree.setLeftNode(node);
		
		rucksack = new Rucksack(100);
		rucksack.insert(o3);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToLeftNode();
		linkedTree.setRightNode(node);

		rucksack = new Rucksack(100);
		rucksack.insert(o4);
		node = new LinkedTreeNode(rucksack);

		linkedTree.moveToRoot();
		linkedTree.setRightNode(node);
		
		
		Rucksack rucksack1 = new Rucksack(100);
		TreeNode node1 = new SequentialTreeNode(rucksack1);
		
		sequentialTree.setCurrentNode(node1);
		
		rucksack1 = new Rucksack(100);
		node1 = new SequentialTreeNode(rucksack1);
		
		sequentialTree.setLeftNode(node1);
		
		rucksack1 = new Rucksack(100);
		rucksack1.insert(o3);
		node = new SequentialTreeNode(rucksack1);

		sequentialTree.moveToLeftNode();
		sequentialTree.setRightNode(node1);

		rucksack1 = new Rucksack(100);
		rucksack1.insert(o4);
		node1 = new SequentialTreeNode(rucksack);

		sequentialTree.moveToRoot();
		sequentialTree.setRightNode(node1);

		

		Assert.assertTrue(sequentialTree.equals(linkedTree));
	}

}
